package com.usian.service;

import com.usian.mapper.LocalMessageMapper;
import com.usian.mapper.TbOrderItemMapper;
import com.usian.mapper.TbOrderMapper;
import com.usian.mapper.TbOrderShippingMapper;
import com.usian.mq.MQSender;
import com.usian.pojo.*;
import com.usian.redis.RedisClient;
import com.usian.utils.JsonUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class OrderService {

    @Value("${ORDER_ID_KEY}")
    private String ORDER_ID_KEY;

    @Value("${ORDER_ID_BEGIN}")
    private Long ORDER_ID_BEGIN;

    @Value("${ORDER_ITEM_ID_KEY}")
    private String ORDER_ITEM_ID_KEY;

    @Autowired
    private RedisClient redisClient;

    @Autowired
    private TbOrderMapper tbOrderMapper;

    @Autowired
    private TbOrderItemMapper tbOrderItemMapper;

    @Autowired
    private TbOrderShippingMapper tbOrderShippingMapper;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private LocalMessageMapper localMessageMapper;

    @Autowired
    private MQSender mqSender;

    @Transactional //添加事务  要么都成功，要么都不成功
    public void insertOrder(OrderInfo orderInfo) {
        String orderItem = orderInfo.getOrderItem();
        TbOrder tbOrder = orderInfo.getTbOrder();
        TbOrderShipping tbOrderShipping = orderInfo.getTbOrderShipping();

        //---------------处理订单对象------------------------
        //在redis保存订单信息
        if (!redisClient.exists(ORDER_ID_KEY)) {
            redisClient.set(ORDER_ID_KEY, ORDER_ID_BEGIN);
        }
        //解决订单号问题
        long ItemId = redisClient.incr(ORDER_ITEM_ID_KEY, 1L);
        tbOrder.setOrderId(ItemId + "");
        Date date = new Date();
        tbOrder.setCreateTime(date);
        tbOrder.setUpdateTime(date);
        //1、未付款，2、已付款，3、未发货，4、已发货，5、交易成功，6、交易关闭
        tbOrder.setStatus(1);
        tbOrderMapper.insertSelective(tbOrder);

        //---------------处理订单的商品信息------------------------
        //当时存的购物车信息是tbitem对象，但是转的时候变成tborderitem对象
        List<TbOrderItem> list = JsonUtils.jsonToList(orderItem, TbOrderItem.class);

        //在redis保存订单信息
        if (!redisClient.exists(ORDER_ITEM_ID_KEY)) {
            redisClient.set(ORDER_ITEM_ID_KEY, 0L);
        }

        for (TbOrderItem tbOrderItem : list) {
            //解决订单号问题
            long OrderItemId = redisClient.incr(ORDER_ITEM_ID_KEY, 1L);
            //订单id
            tbOrderItem.setItemId(ItemId + "");
            //订单中商品id
            tbOrderItem.setId(OrderItemId + "");
            tbOrderItemMapper.insertSelective(tbOrderItem);

        }

        //---------------物流信息------------------------
        tbOrderShipping.setOrderId(ItemId+"");
        tbOrderShipping.setCreated(date);
        tbOrderShipping.setUpdated(date);
        tbOrderShippingMapper.insertSelective(tbOrderShipping);


        //将消息插入到数据库表中，设置消息状态为未成功 0 1
        //消息表中的字段 id 消息内容 消息状态
        LocalMessage localMessage = new LocalMessage();
        localMessage.setTxNo(UUID.randomUUID().toString());
        localMessage.setOrderNo(ItemId+"");
        //0表示为发送成功
        localMessage.setState(0);
        localMessageMapper.insert(localMessage);


        //通过rabbitmq发送信息 给item微服务 进行扣减库存的操作
        //1、交换机的名字 2、routingkey  3、message信息
        //amqpTemplate.convertAndSend("order_exchange","order.add",ItemId);
        mqSender.sendMsg(localMessage);
    }

    public List<TbOrder> selectOverTimeTbOrder() {
        return tbOrderMapper.selectOvertimeOrder();
    }

    public void updateOverTimeTbOrder(TbOrder tbOrder) {
        tbOrder.setStatus(6);
        Date date = new Date();
        tbOrder.setCloseTime(date);
        tbOrder.setEndTime(date);
        tbOrder.setUpdateTime(date);
        tbOrderMapper.updateByPrimaryKey(tbOrder);
    }

    public void updateTbItemByOrderId(String orderId) {
        amqpTemplate.convertAndSend("order_exchange","order.delete",orderId);
    }
}